<?php

namespace McUser\Model;
use Common\Model\CommonModel;

/*
 *  Copyright (c) 2014 The CCP project authors. All Rights Reserved.
 *
 *  Use of this source code is governed by a Beijing Speedtong Information Technology Co.,Ltd license
 *  that can be found in the LICENSE file in the root of the web site.
 *
 *   http://www.yuntongxun.com
 *
 *  An additional intellectual property rights grant can be found
 *  in the file PATENTS.  All contributing project authors may
 *  be found in the AUTHORS file in the root of the source tree.
 */

class KkkUserModel extends CommonModel {

	protected $trueTableName = 'k_user';
	
	/**
	 * 客户押注后, 计算剩余金额.
	 * @param  [type]  $username [description]
	 * @param  [type]  $money    [description]
	 * @param  integer $fanshui  [description]
	 * @return [type]            [description]
	 */
	function cost_money($username,$money,$fanshui=0){
		$sql	=	"select money from k_user where username='".$username."' limit 1";
		$query	=	$this -> query($sql);
		$user	=	$query -> fetch_array();
		$restMoney = $user['money'] - $money + $fanshui;
		if($restMoney >= 0){
			$sql = "update k_user set money=$restMoney where username='".$username."' limit 1";
			$this->query($sql);
			return $restMoney;
		}else{
			return -1;
		}
		
	}

	/**
	 * 更新金额.
	 * @param  [type] $uid [description]
	 * @param  [type] $win [description]
	 * @return [type]      [description]
	 */
	public function update_money($uid, $win){
		$sql = "UPDATE k_user SET money = money + $win WHERE uid = $uid";
		$this -> execute();
	}

}